package cn.remex.mongo;

import cn.remex.core.util.PropertyReader;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.HashMap;
import java.util.Map;

/**
 * 名称：
 * 缩写：
 * 用途：
 * Created by yangy on 2017/1/12 0012.
 */
public class MongoManager {
//	// To directly connect to a single MongoDB server
//	// (this will not auto-discover the primary even if it's a member of a replica set)
//	MongoClient mongoClient = new MongoClient();
//
//	// or
//	MongoClient mongoClient = new MongoClient( "localhost" );
//
//	// or
//	MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
//
//	// or, to connect to a replica set, with auto-discovery of the primary, supply a seed list of members
//	MongoClient mongoClient = new MongoClient(
//			Arrays.asList(new ServerAddress("localhost", 27017),
//					new ServerAddress("localhost", 27018),
//					new ServerAddress("localhost", 27019)));
//
//	// or use a connection string
//	MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017,localhost:27018,localhost:27019");
//	MongoClient mongoClient = new MongoClient(connectionString);
//
//	MongoDatabase database = mongoClient.getDatabase("mydb");


//	// ...
//
//	String user;        // the user name
//	String database;    // the name of the database in which the user is defined
//	char[] password;    // the password as a character array
//	// ...
//	MongoCredential credential = MongoCredential.createCredential(user,
//			database,
//			password);

	//	MongoClientURI uri = new MongoClientURI("mongodb://user1:pwd1@host1/?authSource=db1");
//	private static final Map<String, String> config = PropertyReader.getPropertiesMap("/mrd/serv");
//
//	private static final HashMap<String, MongoClient> clients = new HashMap<>();
////	private static final String env = config.get("ENV");
//	private static final String defaultUri = config.get("/mrd/serv/defaultdb/mongo/url");
//	private static final String defaultDatabase = config.get("/mrd/serv/defaultdb/mongo/dbName");


	private static final Map<String, String> config = PropertyReader.getPropertiesHashMap(MongoManager.class, "/sys-config.properties");

	private static final HashMap<String, MongoClient> clients = new HashMap<>();
	private static final String env = config.get("ENV");
	private static final String defaultUri = config.get(env+".mongo.default");
	private static final String defaultDatabase = config.get(env+".mongo.defaultDb");


	//mongo驱动本身就设计为连接池了，且线程安全，所以此处直接缓存使用
	//后面再考虑多库问题
	public static MongoClient getMongoClient() {
		MongoClient mongoClient = clients.get("default");
		if (null == mongoClient) {
			MongoClientURI connectionString = new MongoClientURI(defaultUri);
			mongoClient = new MongoClient(connectionString);
			clients.put("default", mongoClient);
		}
		return mongoClient;
	}

	public static MongoDatabase getDatabase(String dbName) {
		return getMongoClient().getDatabase(dbName);
	}

	public static MongoDatabase getDatabase() {
		return getDatabase(defaultDatabase);
	}

	public static MongoCvo find(MongoModel modelName) {
		return new MongoCvo(collection(modelName));
	}

	public static MongoCvo _find(String modelName) {
		return new MongoCvo(getDatabase().getCollection(modelName));
	}

	public static MongoCollection<Document> collection(MongoModel modelName) {
		return new MongoCvo(getDatabase().getCollection(String.valueOf(modelName))).getMongoCollection();
	}


}
